<template>
  <yt-page title="重大危险源" :rightData="!enterpriseId ? rightData: []">
    <yt-group :group="false" :searchApi="searchApi" ref="group">
      <yt-cell
        slot-scope="item"
        :label="item.name"
        labelWidth="100%"
        isLink
        @click="goDetailPage(item.id)"></yt-cell>
    </yt-group>

    <yt-popup pos="right" v-model="search" :mask="true" slot="alert">
      <yt-page class="page" :flex="true" :leftData="[]" title="企业列表">
        <yt-form style="background-color: #fff">
          <!-- <yt-radio label="全部" v-model="selected" name="-1" pos="left"></yt-radio> -->
          <yt-radioList class="list" :list="entList" label="EnterpriseName" val="id" v-model="selected"
                        :inline="false"/>
          <yt-btn-group slot="btn" :shadow="false">
            <yt-btn theme="white" @click="search = false">关闭</yt-btn>
            <yt-btn @click="onSearch(selected)">确定</yt-btn>
          </yt-btn-group>
        </yt-form>
      </yt-page>
    </yt-popup>
  </yt-page>
</template>

<script>
  import {commonApi} from "@/common/api/commonApi.js"
  import {SetStore, GetStore, Store, JudgeRole} from '@/store'

  export default {
    data() {
      return {
        search: false,
        rightData: [{
          font: 'yt-icon yt-icon-search',
          click: () => {
            this.search = !this.search
          }
        }],
        entList: [],
        enterpriseId: '',
        selected: '-1',
        searchApi: async ({skip, limit}) => {
          this.enterpriseId = JudgeRole()
            .enterprise(() => {
              return (GetStore(Store.enterprise) || {}).id
            })
            .default()
          let param = {}
          if (this.selected === '-1' && this.enterpriseId) {
            param = {
              equalTo: {
                unit: this.enterpriseId
              }
            }
          } else if (this.selected !== '-1') {
            param = {
              equalTo: {
                unit: this.selected
              }
            }
          } else {
            param = null
          }
          let p = [
            commonApi.search('security_risk_filing', param),
            commonApi.search('ent_enterprise')
          ]
          let [items, entList, hierarchyDic] = await Promise.all(p)
          entList.unshift({id: '-1', EnterpriseName: '全部'})
          this.entList = entList
          return items
        }
      }
    },
    activated () {

    },
    methods: {
      goDetailPage(id) {
        this.$router.$push({path: '/danger-source-check/detail', query: {id: id}})
      },
      onSearch() {
        this.search = false
        this.$refs.group.startPull()
      }
    }
  }
</script>
